package ee.subjects.frontend.controller.command.employeeCommands;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ee.subjects.backend.model.data.Employee;
import ee.subjects.backend.model.data.Employee_role_type;
import ee.subjects.backend.model.data.Ent_per_relation_type;
import ee.subjects.backend.model.data.Enterprise_person_relation;
import ee.subjects.backend.services.EmployeeService;
import ee.subjects.backend.services.EmployeeServiceImpl;
import ee.subjects.backend.services.EnterpriseService;
import ee.subjects.backend.services.EnterpriseServiceImpl;
import ee.subjects.backend.services.Enterprise_person_relation_Service;
import ee.subjects.backend.services.Enterprise_person_relation_ServiceImpl;
import ee.subjects.frontend.controller.command.Command;

public class AjaxEmployeeCommands implements Command {

	@Override
	public int execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String ajaxTask = "undefined";
		PrintWriter out = response.getWriter();
		
		if(request.getParameter("ajaxTask") != null){
			ajaxTask=request.getParameter("ajaxTask");
		}
		
		if(ajaxTask.equals("get_roles_for_person")){
			out.print(this.getRolesForPersonHTML(request));
		}
		
		if(ajaxTask.equals("save_enterprise_subject_relation")){
			out.print(this.saveEnterpriseSubjectRelation(request));
		}
		
		if(ajaxTask.equals("save_person_as_employee")){
			out.print(this.savePersonAsEmployee(request));
		}
		
		if(ajaxTask.equals("delete_employee")){
			out.print(this.deleteEmployee(request));
		}
		
		return 0;
	}

	private String deleteEmployee(HttpServletRequest request) {
		EmployeeService employeeService = new EmployeeServiceImpl();
		
		if(request.getParameter("employeeId")!=null){
			int employeeId = Integer.parseInt(request.getParameter("employeeId"));
			employeeService.deleteEmployeeById(employeeId);
			return "1";
		}else if(request.getParameter("enterpriseId")!=null && request.getParameter("personId")!=null){
			int personId = Integer.parseInt(request.getParameter("personId"));
			int enterpriseId = Integer.parseInt(request.getParameter("enterpriseId"));
			List<Employee> allEmployeeObjectsPersonHas = employeeService.getAllEmployeeObjectsPersonHas(personId);
			for(Employee employee : allEmployeeObjectsPersonHas){
				if(employee.getPerson_fk()==personId && employee.getEnterprise_fk()==enterpriseId){
					int employeeId=employee.getEmployee();
					employeeService.deleteEmployeeById(employeeId);
					return "1";
				}
			}
		}
		return "0";
	}

	private String savePersonAsEmployee(HttpServletRequest request) {
		if(request.getParameter("personId")!=null && request.getParameter("enterpriseId")!=null){
			String personIdStr=request.getParameter("personId");
			int personId = Integer.parseInt(personIdStr);
			
			String enterpriseIdStr=request.getParameter("enterpriseId");
			int enterpriseId = Integer.parseInt(enterpriseIdStr);
			
			EmployeeService employeeService = new EmployeeServiceImpl();
			if(!employeeService.isPersonAlreadyAnEmployeeInAnEnterprise(personId, enterpriseId)){
				employeeService.registerPersonAsAnEnterpriseEmployee(personId, enterpriseId, "Y");
			}else{
				return "Tootaja juba on antud asutuses registreeritud";
			}
			return "1";
			
		}
		return "";
	}

	private String saveEnterpriseSubjectRelation(HttpServletRequest request) {
		if(request.getParameter("personId")!=null && request.getParameter("enterpriseId")!=null && request.getParameter("role")!=null){
			String personIdStr=request.getParameter("personId");
			int personId = Integer.parseInt(personIdStr);
			
			String enterpriseIdStr=request.getParameter("enterpriseId");
			int enterpriseId = Integer.parseInt(enterpriseIdStr);
			
			String role=request.getParameter("role");
			
			// Find role type and role id
			String[] parts = role.split("_");
			String roleType = parts[0];
			String roleIdStr = parts[1];
			
			int roleId = Integer.parseInt(roleIdStr);
			
			if(roleType.equals("partner")){
				
				
			}else if(roleType.equals("employee")){
				EmployeeService employeeService = new EmployeeServiceImpl();
				if(!employeeService.isPersonAlreadyAnEmployeeInAnEnterprise(personId, enterpriseId)){
					employeeService.registerPersonAsAnEnterpriseEmployee(personId, enterpriseId, "Y");
					
				}
				
				// Find inserted employee id
				int employeeId=0;
				List<Employee> personEmployeObjects = employeeService.getAllEmployeeObjectsPersonHas(personId);
				for(Employee employee :personEmployeObjects){
					if(employee.getEnterprise_fk()==enterpriseId){
						employeeId=employee.getEmployee();
						break;
					}
				}
				employeeService.addRoleToEmployee(employeeId, roleId, "Y");
			}
			
		}
		return "";
	}

	private String getRolesForPersonHTML(HttpServletRequest request) {
		if(request.getParameter("personId")!=null){
			String personId=request.getParameter("personId");
			
			EmployeeService employeeService = new EmployeeServiceImpl();
			List<Employee_role_type> employeeRoleTypesList = employeeService.getAllEmployee_role_types();
			
			Enterprise_person_relation_Service enterprisePersonRelationService = new Enterprise_person_relation_ServiceImpl();
			List<Ent_per_relation_type> enterprisePersonRelationTypesList = enterprisePersonRelationService.getAllEnterprise_person_relation_types();
			
			String out="";
			out += "<select style='width: 400px;' id='roleSelectFor_id_"+personId+"'>";
			for(Employee_role_type employeeRoleType :employeeRoleTypesList){
				out += "<option value='employee_"+Integer.toString(employeeRoleType.getEmployee_role_type())+"'>T&ouml;&ouml;suhe: "+employeeRoleType.getType_name()+"</option>";
			}
			for(Ent_per_relation_type enterprisePersonRelation :enterprisePersonRelationTypesList){
				out += "<option value='partner_"+Integer.toString(enterprisePersonRelation.getEnt_per_relation_type())+"'>Partnersuhe: "+enterprisePersonRelation.getType_name()+"</option>";
				
			}
			out += "</select>";
			
			return out;
			
		}
		return "";
	}

}
